package com.startrek.ch02;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.anyOf;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.nullValue;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

class DuplicatedValuesArrayTest2 {
    private DuplicatedValuesArray2 duplicatedValuesArray2;

    @BeforeEach
    void setUp() {
        this.duplicatedValuesArray2 = new DuplicatedValuesArray2();
    }

    @Test
    public void whenDupThenFound() {
        int[] a = new int[] {2, 3, 5, 4, 3, 2, 6, 7};
        Integer result = duplicatedValuesArray2.findDuplicatedValue(a);
        assertThat(result, anyOf(is(2), is(3)));
        int[] b = {1, 2, 3, 4, 5, 6, 6, 7}; // n == 7
        result = duplicatedValuesArray2.findDuplicatedValue(b);
        assertThat(result, is(6));
    }

    @Test
    public void whenNoDupThenNoFound() {
        int[] a = {1, 2, 5, 4, 3, 7, 6};
        Integer result = duplicatedValuesArray2.findDuplicatedValue(a);
        assertThat(result, nullValue());
    }
}